/* 
 * File:   QuitCommand.cpp
 * Author: Bram
 * 
 * Created on January 24, 2013, 7:58 PM
 */

#include "QuitCommand.h"
#include "Config.h"

QuitCommand QuitCommand::cInstance("QUIT");

QuitCommand::QuitCommand(const char* szID )
	: Command( szID )
{
}

QuitCommand::QuitCommand() : Command() {
    setName("QUIT");
}

QuitCommand::QuitCommand(const QuitCommand& orig) {
}

QuitCommand::~QuitCommand() {
}

Command* QuitCommand::createNew()
{
    QuitCommand * cmd = new QuitCommand();
    cmd->setName("QUIT");
    return cmd;
}

bool QuitCommand::isValid()
{
    return ((getName().compare("QUIT") == 0) && Command::isValid());
}

//hier ontvangt de server het commando van de client
void QuitCommand::ExcecuteServerReceiveCommand(TcpSocket* dasServer, queue<Command *>& commandQueue)
{
    cout << getName() << " COMMAND RECEIVED!" << endl;
    dasLog << getName() << " COMMAND RECEIVED!" << endl;
    
}
//hier komt de server nadat hij een bericht van de client heeft ontvangen met een info request
void QuitCommand::ExcecuteServerSendCommand(TcpSocket* dasServer, queue<Command *>& commandQueue)
{
    setType("END");
    addParameter(Config::getInstance()->OkRepresenter);
    addParameter("SERVER QUITED SUCCESSFULLY!");
    addParameter("");
    
    //checkt of het command valide is
    if(!isValid()){
        clearParameters();
        stringstream ssERROR;
        ssERROR << "ERROR 3005 - ERROR BUILDING FULL COMMAND - NOT VALID: ";
        ssERROR << "name: '" << getName()  << "', ";
        ssERROR << "sender: '" << getSender()  << "', ";
        ssERROR << "type: '" << getType()  << "', ";
        addParameter(ssERROR.str());
        //bouwt eerst de error message en past daarna pas de message aan!
        setName("ERROR");
        setType("END");
    }
    string totalCommandStr = buildFullCommand();
    if(totalCommandStr.size() > 10){
        dasServer->sendMessage(totalCommandStr);
        cout << "Server send successfully '" << getName() << "' message to client!" << endl;
        dasLog << "Server send successfully '" << getName() << "' message to client!" << endl;
        //dasServer = 0; //zort ervoor dat de server afsluit!
        //delete[] dasServer;
        commandQueue.push(0);
    } else {
        cout << "ERROR in '" << __FILE__ << "' - '" << __FUNCTION__  << "()', failled on: if(totalCommandStr.size() > 10){ - should not ever happen! 88882" << endl;
        dasLog << "ERROR in '" << __FILE__ << "' - '" << __FUNCTION__  << "()', failled on: if(totalCommandStr.size() > 10){ - should not ever happen! 88882" << endl;
    }
    
}

//dit wordt uitgevoerd als de client het naar de server HEEFT VERSTUURD 
//en gaat wachten op een antwoord!
void QuitCommand::ExcecuteClientReceiveCommand(TcpSocket* dasClient, queue<Command *>& commandQueue)
{
    if(!(getName().compare("ERROR") == 0) ){ //als het geen error is
        //haalt het volgende bericht op!
        Command * receivedCMD = getNextCommandFromSocket(dasClient);
        if(receivedCMD != 0){ //kan eigelijk nooit 0 zijn, wordt afgevangen!
            //als het GEEN error is
            if( !(receivedCMD->getName().compare("ERROR") == 0) ){ //als het geen error is
                //als het commando gelijk is aan het huidige commando!
                if(receivedCMD->getName().compare(getName()) == 0){
                    if(receivedCMD->sizeParameters() > 1){ //als de info command 3 parameters bevat
                        string isOKmessage;
                        string extraMessage;
                        receivedCMD->getParameterAtIndex(0,isOKmessage);
                        receivedCMD->getParameterAtIndex(1,extraMessage);

                        //Config::getInstance()->OkRepresenter
                        cout << "SERVER QUITED: " << isOKmessage << endl;
                        cout << " : " << extraMessage << endl;

                        dasLog << "SERVER QUITED: " << isOKmessage << endl;
                        dasLog << " : " << extraMessage << endl;

                        //delete[] dasClient; //cluit de client!
                        commandQueue.push(0);
                    } else {
                        cout << getName() << " COMMAND FAILLED!!" << endl;
                        dasLog << getName() << " COMMAND FAILLED!!" << endl;
                        receivedCMD->PrintFullCout(); //debug!
                    }
                } else{
                    cout << "ERROR in '" << __FILE__ << "' - '" << __FUNCTION__  << "()', curr cmd: '" << getName() << "', received cmd: '" << receivedCMD->getName() << "' - should not ever happen! 88886" << endl;
                    dasLog << "ERROR in '" << __FILE__ << "' - '" << __FUNCTION__  << "()', curr cmd: '" << getName() << "', received cmd: '" << receivedCMD->getName() << "' - should not ever happen! 88886" << endl;
                }
            } else { //error
                stringstream ssERROR;
                for(int i = 0; i < receivedCMD->sizeParameters(); i++){
                    string strErr;
                    receivedCMD->getParameterAtIndex(0,strErr);
                    ssERROR << strErr << " - ";
                }
                string strTotalErr = ssERROR.str();
                cout << "INFO COMMAND FAILLED! received error: " << strTotalErr << endl;
                dasLog << "INFO COMMAND FAILLED! received error: " << strTotalErr << endl;
            }
        }

        receivedCMD = 0;
        delete receivedCMD;
    }
}

//dit wordt uitgevoerd als de client het naar de server verstuurd
void QuitCommand::ExcecuteClientSendCommand(TcpSocket* dasClient, queue<Command *>& commandQueue)
{
    setType("END");
    //checkt of het command valide is
    if(!isValid()){
        clearParameters();
        stringstream ssERROR;
        ssERROR << "ERROR on sending '" << getName() << "' command to server! Command is nog valid! - ";
        ssERROR << "ERROR 3006 - ERROR BUILDING FULL COMMAND - NOT VALID: ";
        ssERROR << "name: '" << getName()  << "', ";
        ssERROR << "sender: '" << getSender()  << "', ";
        ssERROR << "type: '" << getType()  << "', ";
        cout << ssERROR.str() << endl;
        dasLog << ssERROR.str() << endl;
        
        clearParameters();
        addParameter(ssERROR.str());
        setName("ERROR");
        //@TODO huidigge command error van maken, dat komt dan naar voren in de client receive
    } else { //als alles goed is
        string totalCommandStr = buildFullCommand();
        if(totalCommandStr.size() > 10){
            dasClient->sendMessage(totalCommandStr);
        } else {
            stringstream ss;
            ss << "ERROR in '" << __FILE__ << "' - '" << __FUNCTION__  << "()', if(totalCommandStr.size() > 10){ - should not ever happen! 88887" << endl;
            
            cout << ss.str();
            dasLog << ss.str();
            
            addParameter(ss.str());
            setName("ERROR");
            //@TODO huidigge command error van maken, dat komt dan naar voren in de client receive
        }
    }
}
